package com.hmall.api.config;

import com.hmall.api.client.UserClient;
import com.hmall.api.client.fallback.*;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;


/**
 * @author 宛远鸿
 * @version 1.0
 */
public class DefaultFeignConfig {
    @Bean
    public Logger.Level  feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {// 获取登录用户
                Long userId = UserContext.getUser();
                if(userId != null){// 如果为空则直接跳过
                    template.header("user-info", userId.toString());// 如果不为空则放入请求头中，传递给下游微服务
                }
            }
        };
    }
    @Bean
    public ItemClientFallbackFactory itemClientFallbackFactory(){
        return new ItemClientFallbackFactory();
    }
    @Bean
    public CartClientFallbackFactory cartClientFallbackFactory(){
        return new CartClientFallbackFactory();
    }
    @Bean
    public TradeClientFallbackFactory tradeClientFallbackFactory(){
        return new TradeClientFallbackFactory();
    }
    @Bean
    public UserClientFallbackFactory userClientFallbackFactory(){
        return new UserClientFallbackFactory();
    }
    @Bean
    public PayClientFallback payClientFallback(){return new PayClientFallback();}
}
